import 'package:flutter/material.dart';

class SearchBarWidget extends StatefulWidget {
  final bool showLocation; // 展示位置按钮
  final GestureTapCallback? goBackCallback; // 返回按钮回调
  final String inputValue; // 输入框值
  final String defaultInputValue; // 默认输入框值
  final Function? onCancel; // 取消按钮回调
  final bool showMap; // 展示地图按钮
  final GestureTapCallback? onSearch; // 搜索按钮回调
  final ValueChanged<String>? onSearchSubmit; // 搜索提交回调
  const SearchBarWidget({
    super.key,
    this.showLocation = false,
    this.goBackCallback,
    this.inputValue = '',
    this.defaultInputValue = '请输入搜索词',
    this.onCancel,
    this.showMap = false,
    this.onSearch,
    this.onSearchSubmit,
  });

  @override
  State<SearchBarWidget> createState() => _SearchBarWidgetState();
}

class _SearchBarWidgetState extends State<SearchBarWidget> {
  String _searchWord = '';
  late TextEditingController _controller;
  late FocusNode _focus;

  void _onClean() {
    _controller.clear();
    setState(() {
      _searchWord = '';
    });
  }

  @override
  void initState() {
    _focus = FocusNode();
    _controller = TextEditingController(text: widget.inputValue);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        if (widget.showLocation)
          GestureDetector(
            onTap: () {},
            child: const Padding(
              padding: EdgeInsets.only(right: 10),
              child: Row(
                children: [
                  Icon(Icons.language_outlined),
                  Text('Java'),
                ],
              ),
            ),
          ),
        if (widget.goBackCallback != null)
          GestureDetector(
            onTap: widget.goBackCallback,
            child: const Padding(
              padding: EdgeInsets.only(right: 10),
              child: Icon(Icons.chevron_left),
            ),
          ),
        Expanded(
          child: Container(
            height: 34,
            decoration: BoxDecoration(
              color: Colors.grey[200],
              borderRadius: BorderRadius.circular(17),
            ),
            margin: const EdgeInsets.only(right: 10),
            child: TextField(
              focusNode: _focus,
              controller: _controller,
              style: const TextStyle(
                fontSize: 14,
              ),
              onChanged: (value) {
                setState(() {
                  _searchWord = value;
                });
              },
              onTap: () {
                if (widget.onSearchSubmit == null) {
                  _focus.unfocus();
                }
                if (widget.onSearch != null) {
                  widget.onSearch!();
                }
              },
              onSubmitted: widget.onSearchSubmit,
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: widget.defaultInputValue,
                icon: const Padding(
                  padding: EdgeInsets.only(top: 2, left: 8),
                  child: Icon(
                    Icons.search,
                    size: 18,
                    color: Colors.grey,
                  ),
                ),
                suffixIcon: GestureDetector(
                  onTap: () {
                    _onClean();
                  },
                  child: Icon(
                    Icons.clear,
                    size: 18,
                    color: _searchWord == '' ? Colors.grey[200] : Colors.grey,
                  ),
                ),
                hintStyle: const TextStyle(
                  fontSize: 14,
                  color: Colors.grey,
                ),
                contentPadding: const EdgeInsets.only(top: 1, left: -10),
              ),
            ),
          ),
        ),
        if (widget.onCancel != null)
          GestureDetector(
            onTap: () {},
            child: const Padding(
              padding: EdgeInsets.only(right: 10),
              child: Text(
                '取消',
                style: TextStyle(
                  fontSize: 14,
                ),
              ),
            ),
          ),
        if (widget.showMap) const Icon(Icons.window_rounded),
      ],
    );
  }
}
